package cn.buk.tms.eotms.entity;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import jakarta.persistence.*;


/**
 * 支付订单：通过支付宝或微信支付时，根据对应的订单号生成一个支付订单
 * @author yfdai
 */
@Entity
@Table(name = "payment_order", uniqueConstraints = @UniqueConstraint(name = "uk_payment_order", columnNames = {"trade_no"}))
public class PaymentOrder {

	private static Logger logger = LogManager.getLogger(PaymentOrder.class);
	/**
	 * 付款订单等待付款
	 */
	public final static int PAYMENT_ORDER_WAITING_FOR_PAYMENT = 0;
	/**
	 * 付款订单取消了
	 */
	public final static int PAYMENT_ORDER_CANCELED = 4;
	/**
	 * 付款订单成功：付款成功
	 */
	public final static int PAYMENT_ORDER_SUCCESS = 9;

	@Id
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private int id;

	/**
	 * 本系统生成的交易号，不重复
	 * 支付方式 + 企业ID + “0” + 用户id + "0" + 时间戳
	 * 支付方式： 10-支付宝，20-微信支付
	 */
	@Column(name = "trade_no")
	private String tradeNo;

	/**
	 * 充值的客户
	 */
	@Column(name = "enterprise_id")
	private int enterpriseId;

	/**
	 * 充值客户所属的商家
	 */
	@Column(name = "owner_ent_id")
	private Integer ownerEnterpriseId;


	@Column(name = "product_id")
	private int productId;


	@Column(name = "user_id")
	private int userId;

	private int quantity = 1;

	/**
	 * 订单金额(元）
	 */
	private double price;

	/**
	 * 订单金额（分)
	 */
	@Column(name = "price_fen")
	private Integer priceFen;

	/**
	 * 支付费率
	 */
	@Column(name = "pay_rate")
	private Integer rate;

	/**
	 * 订单状态
	 * 0-待付款
	 * 1-（担保模式）已付款，待发货
	 * 2-（担保模式）已发货，待确认
	 * 3-(即时到帐模式)已付款，待发货
	 * 4-交易取消
	 * 9-交易结束
	 */
	private int status;

	/**
	 * 订单类型：
	 */
	@Column(name = "order_type")
	private Integer orderType;

	/**
	 * 订单类型是大客户订单的话，则保留订单的 id ，方便页面跳转;
	 * 其它情况留空
	 */
	@Column(name = "order_id")
	private Integer orderId;

	/**
	 * 微信支付的工作模式：
	 * 0/null: 默认的公众号支持
	 * 8: 企业微信服务商应用：向授权企业的员工收款
	 */
	@Column(name = "wxp_work_mode")
	private Integer wxpWorkMode;



	@Column(name = "remote_ip", length = 50)
	private String remoteAddress;

	/**
	 * 订单备注
	 */
	@Column(length = 200)
	private String memo;


	//以下为支付宝付款返回的信息

	/**
	 * 支付宝上的交易号
	 */
	@Column(name = "alipay_trade_no", length = 30)
	private String alipayTradeNo;

	/**
	 * 支付宝上买家的email
	 */
	@Column(name = "alipay_buyer_email", length = 100)
	private String alipayBuyerEmail;

	/**
	 * 支付宝上买家的id
	 */
	@Column(name = "alipay_buyer_id", length = 20)
	private String alipayBuyerId;



	// 以下为微信支付返回的内容

	/**
	 * 公众账号ID
	 */
	@Column(length = 32, name = "wx_appid")
	private String appid;

	/**
	 * 微信支付商家id
	 */
	@Column(length = 32, name = "wx_mchid")
	private String mchId;

	@Column(length = 32, name = "wx_device_info")
	private String deviceInfo;

	@Column(length = 128, name = "wx_openid")
	private String openid;

	/**
	 * 微信支付订单号
	 */
	@Column(length = 32, name = "wx_transaction_id")
	private String transactionId;

	/**
	 * 付款银行
	 */
	@Column(length = 16, name = "wx_bank_type")
	private String bankType;

	/**
	 * 现金支付金额
	 */
	@Column(name = "wx_cash_fee")
	private Integer cashFee;

	/**
	 * 支付完成时间
	 */
	@Column(length = 14, name = "wx_time_end")
	private String timeEnd;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "create_time", insertable = false, updatable = false)
	private Date createDate;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public int getProductId() {
		return productId;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public String getRemoteAddress() {
		return remoteAddress;
	}

	public void setRemoteAddress(String remoteAddr) {
		if (remoteAddr != null) {
			this.remoteAddress = remoteAddr.trim();

			if (this.remoteAddress.length() > 50) {
				this.remoteAddress = this.remoteAddress.substring(0, 50);
			}

			if (this.remoteAddress.length() > 15) {
				logger.warn(this.remoteAddress);
			}
		}
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	public String getAlipayTradeNo() {
		if (alipayTradeNo == null) {
			alipayTradeNo = "";
		}
		return alipayTradeNo;
	}

	public void setAlipayTradeNo(String alipayTradeNo) {
		this.alipayTradeNo = alipayTradeNo;
	}

	public String getAlipayBuyerEmail() {
		if (alipayBuyerEmail == null) {
			alipayBuyerEmail = "";
		}
		return alipayBuyerEmail;
	}

	public void setAlipayBuyerEmail(String alipayBuyerEmail) {
		this.alipayBuyerEmail = alipayBuyerEmail;
	}

	public String getAlipayBuyerId() {
		if (alipayBuyerId == null) {
			alipayBuyerId = "";
		}
		return alipayBuyerId;
	}

	public void setAlipayBuyerId(String alipayBuyerId) {
		this.alipayBuyerId = alipayBuyerId;
	}


	/**
	 * @return the memo
	 */
	public String getMemo() {
		return memo == null ? "": memo.trim();
	}

	/**
	 * @param memo the memo to set
	 */
	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * @return the price
	 */
	public double getPrice() {
		return price;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(double price) {
		this.price = price;
	}

	public String getTradeNo() {
		return tradeNo;
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

	public int getEnterpriseId() {
		return enterpriseId;
	}

	public void setEnterpriseId(int enterpriseId) {
		this.enterpriseId = enterpriseId;
	}

	public Integer getPriceFen() {
		return priceFen;
	}

	public void setPriceFen(Integer priceFen) {
		this.priceFen = priceFen;
	}

	public void setOwnerEnterpriseId(int ownerEnterpriseId) {
		this.ownerEnterpriseId = ownerEnterpriseId;
	}

	public int getOwnerEnterpriseId() {
		return ownerEnterpriseId;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getDeviceInfo() {
		return deviceInfo;
	}

	public void setDeviceInfo(String deviceInfo) {
		this.deviceInfo = deviceInfo;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}

	public String getBankType() {
		return bankType;
	}

	public void setBankType(String bankType) {
		this.bankType = bankType;
	}

	public Integer getCashFee() {
		return cashFee;
	}

	public void setCashFee(Integer cashFee) {
		this.cashFee = cashFee;
	}

	public String getTimeEnd() {
		return timeEnd;
	}

	public void setTimeEnd(String timeEnd) {
		this.timeEnd = timeEnd;
	}

	public Integer getOrderType() {
		return orderType;
	}

	public void setOrderType(Integer orderType) {
		this.orderType = orderType;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public Integer getRate() {
		return rate;
	}

	public void setRate(Integer rate) {
		this.rate = rate;
	}

	public int getWxpWorkMode() {
		return wxpWorkMode == null ? 0 : wxpWorkMode;
	}

	public void setWxpWorkMode(Integer wxpWorkMode) {
		this.wxpWorkMode = wxpWorkMode;
	}
}

